from rest_framework import serializers

from apps.hourses.models import Area, House
from utils.send_message import url


class AreaSerializer(serializers.ModelSerializer):
    aid = serializers.IntegerField(source='id')
    aname = serializers.CharField(source='name')
    class Meta:
        model = Area
        fields = ['aid','aname']



class HouseSerializer(serializers.ModelSerializer):
    house_id = serializers.IntegerField(source='id')
    area_name = serializers.StringRelatedField(source='area.name',read_only=True,required=False)
    img_url = serializers.SerializerMethodField()
    user_avatar = serializers.CharField(source='user.avatar',required=False)
    ctime= serializers.DateTimeField(source='create_time',format='%Y-%M-%d',required=False)
    def get_img_url(self,obj):
        return url + obj.index_image_url
    def get_user_avatar(self,obj):
        return url +str(obj.user.avatar)
    class Meta:
        model = House
        fields = ['house_id','order_count','title','ctime',
                  'ctime','price','area_name','address','room_count','img_url','user_avatar'
                  ]